package com.example.activity_usage.datatrans;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.activity_usage.MainActivity;
import com.example.activity_usage.R;
import com.example.activity_usage.utils.DateUtils;

public class SendDataActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView textSend;

    private TextView textResp;

    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_send_data);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 单向传递数据
        textSend = findViewById(R.id.text_send);

        Button btnSend = findViewById(R.id.btn_send);
        btnSend.setOnClickListener(this);

        // 双向传递数据，跳转到目标Activity时传递数据过去，从目标Activity返回时接收响应数据
        textResp = findViewById(R.id.text_resp);
        Button btnSendWithResp = findViewById(R.id.btn_send_with_resp);
        btnSendWithResp.setOnClickListener(this);
        // 注册响应数据的处理回调
        register = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult o) {
                        // 处理响应结果
                        Intent respIntent = o.getData();
                        if (respIntent != null && respIntent.getExtras() != null) {
                            Bundle respData = respIntent.getExtras();
                            String responseTime = respData.getString("respTime");
                            String responseContent = respData.getString("respContent");
                            textResp.setText(String.format("返回了响应数据。\n响应时间：%s\n响应内容：%s", responseTime, responseContent));
                        }
                    }
                }
        );

        PackageManager packageManager = getPackageManager();
        try {
            ActivityInfo activityInfo = packageManager.getActivityInfo(
                    getComponentName(), PackageManager.GET_META_DATA);
            String developerId = activityInfo.metaData.getString("developerId");
            Log.d("SendDataActivity", String.format("开发者Id=%s", developerId));
        } catch (Exception e) {

        }

    }

    @Override
    public void onClick(View v) {
        // 将要传递到下一个Activity的数据打包到Bundle中
        String text = textSend.getText().toString();
        Bundle dataBundle = new Bundle();
        dataBundle.putString("requestTime", DateUtils.getFormattedTimeNow());
        dataBundle.putString("requestContent", text);
        if (v.getId() == R.id.btn_send) {
            // 单向传递数据
            // 创建跳转的Intent
            Intent intent = new Intent(this, ReceiveDataActivity.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
        } else if (v.getId() == R.id.btn_send_with_resp) {
            // 双向传递数据
            Intent intent = new Intent(this, ReceiveDataActivity.class);
            intent.putExtras(dataBundle);
            register.launch(intent);
        }
    }
}